package com.sale.succ.components;

import com.sale.succ.utils.TraderConstants;

import eve.fx.Insets;
import eve.ui.Gui;

public class MessDialog extends DefForm {
	private int type = OK;
	private DefButton okButton;
	private String message = "";

	// types
	public static final int OK = 0;
	public static final int NOB = 1;

	public MessDialog(String mess) {
		this.title = "";
		backGround = TraderConstants.MFORM_BGR;
		setBorder(EDGE_BUMP | BDR_OUTLINE, 2);
		this.message = mess;
		defaultTags.set(TAG_INSETS, new Insets(5, 5, 5, 5));
		exitButtonDefined = false;
		create();
	}

	public MessDialog(String mess, int type) {
		this.title = "";
		this.type = type;
		backGround = TraderConstants.MFORM_BGR;
		setBorder(EDGE_BUMP | BDR_OUTLINE, 2);
		this.message = mess;
		defaultTags.set(TAG_INSETS, new Insets(5, 5, 5, 5));
		exitButtonDefined = false;
		create();
	}

	public void showAsPopup() {
		exec(Gui.topFrame(this), Gui.CENTER_FRAME | Gui.PUTTING_POPUP);
		waitUntilClosed();
	}

	private DefButton getOkButton() {
		if (okButton == null) {
			okButton = new DefButton("OK");
			okButton.setPreferredSize(40, 20);
			okButton.action = TraderConstants.OK_ACTION;
		}
		return okButton;
	}

	private DefLabel createLabel(String txt) {
		DefLabel message = new DefLabel(txt);
		message.font = TraderConstants.LABEL_FONT_SMALL;
		return message;
	}

	private void create() {
		switch (type) {
		case NOB:
			addLast(createLabel(message)).setControl(HCENTER).setCell(HGROW);
			break;
		case OK:
		default:
			addLast(createLabel(message)).setControl(HCENTER).setCell(HGROW);
			addLast(getOkButton()).setControl(HCENTER).setCell(HGROW);
			break;
		}
	}

	public boolean handleAction(String action) {
		if (action.equals(TraderConstants.OK_ACTION)) {
			exit(1);
			return true;
		}
		return super.handleAction(action);
	}

	protected boolean canExit(int exitCode) {
		return exitCode != 0 && exitCode != -1;
	}
}
